package com.swt.gap.service.impl;

import com.swt.gap.dao.AchievementClassDao;
import com.swt.gap.dao.AchievementDao;
import com.swt.gap.model.entity.AchievementClass;
import com.swt.gap.service.AchievementClassService;
import com.swt.gap.service.SysGroupService;
import com.swt.gap.util.UserLoginUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author sx
 */
@Service
public class AchievementClassServiceImpl implements AchievementClassService {

    private final AchievementClassDao achievementClassDao;
    private final AchievementDao achievementDao;
    private final SysGroupService sysGroupService;

    public AchievementClassServiceImpl(AchievementClassDao achievementClassDao,
                                       AchievementDao achievementDao,
                                       SysGroupService sysGroupService) {
        this.achievementClassDao = achievementClassDao;
        this.achievementDao = achievementDao;
        this.sysGroupService = sysGroupService;
    }

    @Override
    public List<AchievementClass> listAllAchievementClass() {
        int userId = UserLoginUtils.getUserId();
        List<Integer> userIds = sysGroupService.listSameGroupUserByUserId(userId);
        return achievementClassDao.selectAll(userIds);
    }

    @Override
    public Integer addAchievementClass(AchievementClass record) {
        record.setCreatedTime(new Date());
        return achievementClassDao.insertSelective(record);
    }

    @Override
    public Integer updateAchievementClass(AchievementClass record) {
        return achievementClassDao.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteAchievementClassAndAchievement(Integer achievementClassId) {
        achievementDao.deleteAchievementByClassId(achievementClassId);
        return achievementClassDao.deleteByPrimaryKey(achievementClassId);
    }
}
